const $cache = require('@src/common/wxCache.js')
const extConfig = wx.getExtConfigSync();
const urlPrefix = extConfig.requestDomain;
const appId = extConfig.appId;
const businessId = extConfig.businessId

const TOKEN_KEY = "token"
const ANON_TOKEN_KEY = "anonUser"

// 定义跳转webview的路径
exports.webviewPath = "/pages/h5-login/index";

exports.loginApi = (data) => {
  return new Promise((reslove, reject) => {
    wx.request({
      url: urlPrefix + '/uaa/wxapp/login',
      data: data,
      method: "POST",
      header: {
        'Content-Type': 'application/json; charset=UTF-8',
        'X-APP-ID': appId,
        'X-BUSINESS-ID': businessId,
      },
      success(res) {
        reslove(res)
      },
      fail(error) {
        reject(error)
      }
    })
  })
}


const getToken = () => {
  return $cache.get(TOKEN_KEY)
}

const  setToken = (token, expireTime) => {
  $cache.setSync(TOKEN_KEY, token, ~~(expireTime/1000))
}

const  getAnon = () => {
  return $cache.get(ANON_TOKEN_KEY)
}

const setAnon = (isAnon, expireTime) => {
  return $cache.setSync(ANON_TOKEN_KEY, isAnon, ~~(expireTime/1000))
}


exports.getToken = getToken;
exports.setToken = setToken;
exports.setAnon = setAnon;
exports.getAnon = getAnon;
exports.isAnonUser = () => {
  return !!getAnon();
}

exports.isLogin = () => {
  /**!! 有值就是true 没值就是false  */ 
  return !getAnon() && !!getToken();
}
